package com.heatup.apt.management.service.coach.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.model.coach.CoachCourseFeeVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.common.dao.CoachCourseBasicFeeMapper;
import com.heatup.apt.common.dao.CoachCourseFeeMapper;
import com.heatup.apt.common.dao.CoachSigninLogMapper;
import com.heatup.apt.common.dao.CourseSignMapper;
import com.heatup.apt.management.service.coach.CoachCourseFeeService;
import com.heatup.apt.model.coach.CampCountFeeDetail;
import com.heatup.apt.model.coach.CoachCourseBasicFee;
import com.heatup.apt.model.coach.CoachCourseFee;
import com.heatup.apt.model.course.CoursePrivteFeeVO;

/**
 * CoachCourseFeeVO 表数据服务层接口实现类
 */
@Service
public class CoachCourseFeeServiceImpl extends SuperServiceImpl<CoachCourseFeeMapper, CoachCourseFee> implements CoachCourseFeeService {

    public static final Logger log = LoggerFactory.getLogger(CoachCourseFeeServiceImpl.class);

    @Autowired
    private CoachCourseFeeMapper coachCourseFeeMapper;
    @Autowired
    private CoachCourseBasicFeeMapper coachCourseBasicFeeMapper;
    
    @Autowired
	private CourseSignMapper courseSignMapper;
    
    @Autowired
    CoachSigninLogMapper coachSigninLogMapper;

    /**
     * 保存教练课时费
     *
     * @param coachCourseFee 课时费
     * @return true 成功 / false 失败
     */
    @Transactional
    @Override
    public boolean save(CoachCourseFee coachCourseFee) {
        if (coachCourseFee != null) {

            if (StringUtils.isNotBlank(Arrays.toString(coachCourseFee.getStandCourse()))) {

                CoachCourseBasicFee teamBasicFee = new CoachCourseBasicFee();
                teamBasicFee.setCoachId(coachCourseFee.getCoachId());
                coachCourseBasicFeeMapper.deleteSelective(teamBasicFee);//如果有旧数据先做移除操作
                List<CoachCourseBasicFee> basicFeeList = new ArrayList<>();
                for (int i = 0; i < coachCourseFee.getStandCourse().length; i++) {
                    String[] tempStandBasicCourseFeeAndFee = coachCourseFee.getStandCourse()[i].split("=");
                    for (String s : tempStandBasicCourseFeeAndFee[1].split("&")) {

                        teamBasicFee = new CoachCourseBasicFee();
                        teamBasicFee.setCoachId(coachCourseFee.getCoachId());
                        teamBasicFee.setTeamCourseBasicFee(new BigDecimal(coachCourseFee.getStandCourse()[i].split("=")[0]));
                        teamBasicFee.setTeamCourseStandCourseId(Integer.valueOf(s));
                        teamBasicFee.setCreatedTime(new Date());
                        basicFeeList.add(teamBasicFee);
                    }
                }
                boolean basicFeeSuc = false;
                if (basicFeeList.size() > 0) {
                    basicFeeSuc = coachCourseBasicFeeMapper.insertBatch(basicFeeList) > 0;
                }
                if (basicFeeSuc) {
                    //团课基础课时费关联数据保存成功才保存主信息
                    if (coachCourseFee.getId() == null) {
                        coachCourseFee.setCreatedTime(new Date());
                        return coachCourseFeeMapper.insertSelective(coachCourseFee) > 0;
                    } else {
                        coachCourseFee.setUpdatedTime(new Date());
                        return coachCourseFeeMapper.updateSelectiveById(coachCourseFee) > 0;
                    }
                } else {
                    throw new RuntimeException("保存教练团课基础课时费数据出错");
                }
            }
        }
        return false;
    }
    
    @Override
    public List<CoursePrivteFeeVO> queryCoachPrivateFeeDetail(Map<String, Object> paramsMap) {
    	return courseSignMapper.queryPrivateCoachFee(paramsMap);
    }

	@Override
	public Map<String, Object> queryCoachCampFeeDetail(Map<String, Object> paramsMap) {
		Map<String, Object> map = new HashMap<>();
		List<CampCountFeeDetail> list = coachSigninLogMapper.queryCampCoachFee(paramsMap);
		int totalCount = coachSigninLogMapper.queryCampCoachCount(paramsMap);
		map.put("campList", list);
		map.put("campCount", totalCount);
		return map;
	}
    

    /**
     * 根据教练 ID 查询教练课时费详情
     *
     * @param coachId 教练 ID
     * @return 返回教练课时费详情
     */
    @Override
    public CoachCourseFee queryByCoachId(Integer coachId) {
        CoachCourseFee coachCourseFeeQuery = new CoachCourseFee();
        coachCourseFeeQuery.setCoachId(coachId);
        return coachCourseFeeMapper.selectOne(coachCourseFeeQuery);
    }

    /**
     * 教练课时费分页
     *
     * @param coachCourseFee 教练
     * @return 教练课时费分页
     */
    @Override
    public JSONObject page(CoachCourseFeeVO coachCourseFee) {
        if (coachCourseFee.getCoachId() == null || coachCourseFee.getPageIndex() == null || coachCourseFee.getPageSize() == null) {
            return null;
        }
        if (coachCourseFee.getPageIndex() > 0) {
            coachCourseFee.setPageIndex(coachCourseFee.getPageIndex()-1);
            coachCourseFee.setPageIndex(coachCourseFee.getPageIndex() * coachCourseFee.getPageSize());
        }
        JSONObject resultJson = new JSONObject();
        resultJson.put("list", coachCourseFeeMapper.pageList(coachCourseFee));
        resultJson.put("count", coachCourseFeeMapper.pageCount(coachCourseFee));

        resultJson.put("totalTotalSum", this.queryCoachFeeTotalSum(coachCourseFee));
        return resultJson;
    }

    /**
     * 查询教练所有课时费总计
     *
     * @param courseFeeVO 教练 ID 年/月 参数
     * @return 返回教练所有课时费总计
     */
    @Override
    public BigDecimal queryCoachFeeTotalSum(CoachCourseFeeVO courseFeeVO) {

        if (courseFeeVO == null || courseFeeVO.getCoachId() == null ) return null;
        courseFeeVO.setPageIndex(null);
        courseFeeVO.setPageSize(null);
        List<CoachCourseFeeVO> courseFeeVOList = coachCourseFeeMapper.pageList(courseFeeVO);
        BigDecimal totalTotalSum = new BigDecimal(0);
        for (CoachCourseFeeVO feeVO : courseFeeVOList) {
            totalTotalSum = totalTotalSum.add(feeVO.getTotalSum());
        }
        return totalTotalSum;
    }
}